package sample.web.filter;

import org.apache.commons.lang3.ArrayUtils;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by zhaopeng on 3/1/16.
 */
public class UserInfoFilter implements Filter{
    private final static String[] whiteList = {"/user/add", "/user/edit", "/user/delete"};
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        String requestURI = request.getRequestURI();
        System.out.println(requestURI);
        //管理员不拦截
                if (ArrayUtils.contains(whiteList, requestURI)) {
                int roleid = (Integer) request.getSession().getAttribute("roleid");
                if (roleid==1){
                    filterChain.doFilter(request,response);
                    return;
                }else {
                    response.sendRedirect("/user/noFound");
                    return;
                }
            }
        filterChain.doFilter(request,response);
    }

    @Override
    public void destroy() {

    }
}
